/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;
/*
 * 
 * SocketTCPServer
 * Matthieu PERRIN
 * Interface reseau (TCP) pour que les clients puissent interoger le gestionnaire de media
 * 
 */
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketTCPServer
{
    private  int           _port = 4004;
    private  ServerSocket  _socket; 
    private mediaManager manager;
    private String IP_client;
    
    public SocketTCPServer(mediaManager manager, int port)
    {
        this.manager = manager;
        this._port = port;
        Server();
    }
    

    public  void Server()
    {
        try {
            _socket = new ServerSocket(_port);

            System.out.println("TCP server is running on " + _port + "...");

            while (true) {
                                
                // Attente d'un client
                Socket client = _socket.accept();
                InputStream input = client.getInputStream();
                
                //System.out.print("\nConnection du client : " + client.getInetAddress().toString());
                IP_client = client.getInetAddress().toString().replace("/", "");
                System.out.print("\r\nConnection du client : " + IP_client);
                
                BufferedReader br = new BufferedReader(new InputStreamReader(input));

                // Lecture et traitement des datas recues
                String reponse = Traitement(br.readLine()) + '\n';

                OutputStream output = client.getOutputStream();

                // reponse au client
                output.write(reponse.getBytes());
                client.close();
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                _socket.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    
    private String Traitement(String input)
    {
        /*
         * Le 1er octet corresspond a un numero de message :
         *  1 = GetProvider
         *  2 = SetProvider
         *  3 = DelProvider
         */
        
        System.out.print(" - Requete : \"" + input + "\"");
        
        String output= "";
        
        // SI code = 1 : GetProvider
        if (input.charAt(0) == '1')
        {
            String id = input.substring(1);
            output = "2" + manager.getProvider(id);
        }
        // Si code = 3 : setProvider
        else if (input.charAt(0) == '3')
        {
            String data = input.substring(1);
            String id = data.substring(0, data.indexOf('+'));
            String ip = data.substring(data.indexOf('+')+1);
            manager.setProvider(id, IP_client);
            //manager.setProvider(id, ip);
            output = "4";
        }
        // Si code = 5 : delProvider
        else if (input.charAt(0) == '5')
        {
            String data = input.substring(1);
            String id = data.substring(0, data.indexOf('+'));
            String ip = data.substring(data.indexOf('+')+1);
            manager.DelProvider(id, IP_client);
            manager.DelProvider(id, ip);
            output = "6";
        }
        else
        {
            output = "9 error";
        }
        
        System.out.print(" - Reponse : \"" + output + "\"");
        return output;
    }
}
